/**
 *   This file is part of Java Simple Sampler.
 *
 *   Java Simple Sampler is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License.
 *
 *   Java Simple Sampler is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Java Simple Sampler.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   @author David Lapetina	
 */
package net.onirik.mao.sampler.sound;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.AudioFormat.Encoding;

public class AudioHelper {

	private static final AudioFormat audioFormat = new AudioFormat(
			Encoding.PCM_SIGNED, 22050f, 16, 2, 4, 22050f, false);

	private static final DataLine.Info info = new DataLine.Info(
			SourceDataLine.class, audioFormat);

	public static AudioHelper SINGLETON = new AudioHelper();

	public AudioHelper() {
	}

	public AudioInputStream getAudioInputStream(File file) {
		try {
			return AudioSystem.getAudioInputStream(file);
		} catch (UnsupportedAudioFileException e1) {
			throw new IllegalArgumentException(e1);
		} catch (IOException e1) {
			throw new IllegalArgumentException(e1);
		}
	}

	public AudioFormat getAudioFormat() {
		return audioFormat;
	}

	public SourceDataLine getSourceDataLine() {
		try {
			SourceDataLine auline = (SourceDataLine) AudioSystem.getLine(info);
			auline.open(audioFormat);
			auline.start();
			return auline;
		} catch (LineUnavailableException e) {
			throw new IllegalArgumentException(e);
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		}
	}
}
